# !/usr/bin/env python
# -*- coding: utf-8 -*-

"""
A股预披露producer
修改配置文件说明：

- 通过父文件中的config.cfg中的[publish]下的pre_time_range配置所发送公告的发布时间区间,
    例如把[publish]下的pre_time_range = 1995-01-01 00:00:00 ~ 2016-09-01 00:00:00

- 通过父文件中的config.cfg中的[queue]下的pre_queue_name配置所发送到的queue,
    例如把[queue]下的pre_queue_name = stockProj_temp_pre
"""

import sys
import os
from datetime import datetime
from ConfigParser import ConfigParser

from anno_producer import BuildPreAShare
from anno_producer import produce_message


if __name__ == '__main__':
    config = ConfigParser()
    config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'config.cfg'))
    with open(config_path, 'r') as cfgfile:
        config.readfp(cfgfile)

    time_range = config.get('publish', 'pre_time_range').split('~')
    # STARTUP_TIME = datetime.strptime(config.get('startUp_time', 'startUP_time'), '%Y-%m-%d %H:%M:%S')
    STARTUP_TIME = datetime.today()  # 程序启动时间 eg: datetime.datetime(2016, 9, 14, 13, 53, 4)

    # A股预披露
    build_message = BuildPreAShare(start_anno_time=time_range[0].strip(), stop_anno_time=time_range[1].strip(), STARTUP_TIME=STARTUP_TIME)

    amqp_url = 'amqp://sc-admin:1qaz2wsx@10.51.29.242:5672/%2F?connection_attempts=3&heartbeat_interval=3600'
    queue_name = config.get('queue', 'pre_queue_name')
    produce_message(amqp_url, queue_name, build_message)